package com.paimeng.lucence;


import com.paimeng.utils.DBStatement;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
 * @author: 潘辉
 * @Title: CreateIndex
 * @ProjectName: paimeng
 * @Description: 建立索引
 * @date: 2021/6/17 16:43
 */

public class CreateIndex {

    public static void main(String[] args) throws SQLException, IOException {
        // TODO Auto-generated method stub
        Connection con = DBStatement.getDbConnection();
        String sql = "select * from news_info;";
        ResultSet records = DBStatement.getResultSet(con, sql);

        getIndex(records);

    }

    // 创建索引过程：
    //1 创建文档对象
    //2 创建存储目录
    //3 创建分词器
    //4 创建索引写入器的配置对象
    //5 创建索引写入器对象
    //6 将文档交给索引写入器
    //7 提交
    //8 关闭
    public static void getIndex(ResultSet records) throws SQLException, IOException {


        //2、建立索引目录类，指明索引在硬盘中的位置
        File indexpath = new File("src/main/webapp/index");
        if (indexpath.exists() != true) {
            indexpath.mkdirs();
        }
        Path dir = Paths.get("src/main/webapp/index");
        //FSDirectory：文件系统目录，会把索引库指向本地磁盘。
        Directory directory = FSDirectory.open(dir);
        //3、创建分词器对象
        Analyzer analyzer = new StandardAnalyzer();
        //4 索引写出工具的配置对象
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
        indexWriterConfig.setOpenMode(OpenMode.CREATE);
        //5 创建索引的写出工具类。参数：索引的目录和配置信息
        IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);


        int i = 0;
        while (records.next()) {
            System.out.println(records.getString("title") + records.getString("url")
                    + records.getString("date") + records.getString("srcFrom") + records.getString("content") + records.getString("comment") + records.getString("hotIndex"));

            //1、创建文档对象
            Document doc = new Document();
            // 创建并添加字段信息。参数：字段的名称、字段的值、是否存储，这里选Store.YES代表存储到文档列表。Store.NO代表不存储
            // 这里我们title字段需要用TextField，即创建索引又会被分词。
            doc.add(new TextField("title", records.getString("title"), Field.Store.YES));
            doc.add(new TextField("url", records.getString("url"), Field.Store.YES));
            doc.add(new TextField("date", records.getString("date"), Field.Store.YES));
            doc.add(new TextField("content", records.getString("content"), Field.Store.YES));
            doc.add(new TextField("hotIndex", records.getString("hotIndex"), Field.Store.YES));
//			doc.add(new TextField("editor", records.getString("editor"), Field.Store.YES));
            //6 把文档交给IndexWriter
            indexWriter.addDocument(doc);
        }
        //7、提交
        indexWriter.commit();
        System.out.println("numDocs" + indexWriter.numRamDocs());
        //8、关闭
        indexWriter.close();
        directory.close();
    }


}
